// pages/cart/dynamics.js
var util = require('../../../utils/util.js');
import LoadMore from '../../../utils/LoadMore.js';
const load = new LoadMore;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    currentPage: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    load.init(this, '', 'recommend');
    this.getArticleList();
  },

  getArticleList(type) {
    let that = this;
		let requestUrl = '/api/Article/getList?p=' + that.data.currentPage;
		load.request(requestUrl, function (res) {
      wx.stopPullDownRefresh();
      let result = res.data.result || [];
			if (type == 'more') {
        if(result.length == 0) {
          that.data.currentPage--;
        } else {
          result.forEach(function(value) {
            value.publish_time = util.format(value.publish_time);
          })
        }
				that.setData({
					list: [...that.data.list, ...result]
        })
			} else {
        if(result.length > 0) {
          result.forEach(function(value) {
            value.publish_time = util.format(value.publish_time);
          })
        }
        that.setData({
          list: result
        })
      }
		});
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.data.list = [];
		this.data.currentPage = 1;
		this.getArticleList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.data.currentPage++;
		this.getArticleList('more')
  },

  // 跳转详情页
  toDetail(e) {
    var data = this.data.list[e.currentTarget.dataset.index];
    var content = encodeURIComponent(data.content);
    console.log(content)
    wx.navigateTo({
      url: '/pages/cart/dynamice_detail/dynamice_detail?title='+data.title+'&content='+content+'&click='+data.click+'&publish_time='+data.publish_time
    })
  }
})